package com.fellan.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.Menu;
import android.view.MenuItem;
import com.fellan.MainActivity;
import com.fellan.R;
import com.fellan.SimpleItemizedOverlay;
import com.fellan.business.BusinessConstants;
import com.fellan.business.DataManager;
import com.fellan.model.Line;
import com.fellan.model.Station;
import com.fellan.persistance.DBManager;
import com.fellan.persistance.PersistenceConstants;
import com.google.android.maps.*;
import com.kankan.wheel.widget.adapters.ArrayWheelAdapter;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Edy
 * Date: 27.05.2012
 * Time: 06:05
 * To change this template use File | Settings | File Templates.
 */
public class Harta extends MapActivity {

    MapView mapView;
    List<Overlay> mapOverlays;
	Drawable drawable;
    Drawable drawable2;
	SimpleItemizedOverlay itemizedOverlay;
    SimpleItemizedOverlay itemizedOverlay2;

    IntentFilter receivedAllStationsSuccessIntentFilter;
    //IntentFilter receivedLinesForStationsSuccessIntentFilter;

    BroadcastReceiver receivedAllStationsSuccessReceiver;
    //BroadcastReceiver receivedLinesForStationsSuccessReceiver;

    Hashtable<Station, ArrayList<Line>> hashtable = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        final Bundle savedInstanceState2 = savedInstanceState;
        setContentView(R.layout.hartax);
        ActivityUtils.showProgressDialog(Harta.this);

        mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);

		mapOverlays = mapView.getOverlays();

		// first overlay
		drawable = getResources().getDrawable(R.drawable.marker);
        drawable2 = getResources().getDrawable(R.drawable.marker2);
		itemizedOverlay = new SimpleItemizedOverlay(drawable, mapView);
        itemizedOverlay2 = new SimpleItemizedOverlay(drawable2, mapView);

        if(receivedAllStationsSuccessIntentFilter == null)
            receivedAllStationsSuccessIntentFilter = new IntentFilter(PersistenceConstants.PC_ALL_STATIONS_WITH_LINES_RECEIVED_SUCCESSFULLY);
        if(receivedAllStationsSuccessReceiver == null)
            receivedAllStationsSuccessReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    context.unregisterReceiver(receivedAllStationsSuccessReceiver);
                    hashtable = DBManager.getInstance().hashtable;

                    LocationManager lm = (LocationManager) DataUtils.getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
                    Criteria criteria = new Criteria();
                    String bestProvider = lm.getBestProvider(criteria, false);
                    Location loc = lm.getLastKnownLocation(bestProvider);
                    Location location = null;
                    if (loc != null)
                       location = new Location(loc);

                    float distance = 655350000;
                    GeoPoint apr = null;

                    for (Station s : hashtable.keySet())
                    {
                        ArrayList<Line> lines = hashtable.get(s);
                        String linii = "Linii:";
                        for (int i=0; i<lines.size(); i++)
                        {
                            linii = linii + " "  + lines.get(i).getName();
                            if (i!=lines.size()-1)
                                linii+=",";
                        }
                        GeoPoint point = new GeoPoint((int)(s.getLat()*1E6),(int)(s.getLng()*1E6));
                        OverlayItem item = new OverlayItem(point, s.getName(),linii);
                        itemizedOverlay.addOverlay(item);

                        if (loc != null)
                        {
                            location.setLatitude(s.getLat());
                            location.setLongitude(s.getLng());
                        }
                        if (!DataUtils.showPoint && loc != null && loc.distanceTo(location)<distance)
                        {
                            distance = loc.distanceTo(location);
                            apr = point;
                        }
                        else if (loc == null)
                            apr = point;
                    }

                    mapOverlays.add(itemizedOverlay);

                    if (loc != null)
                    {
                        itemizedOverlay2.addOverlay(new OverlayItem(new GeoPoint
                                ((int)(loc.getLatitude()*1E6),(int)(loc.getLongitude()*1E6)),
                                "Pozitia ta",""));

                        mapOverlays.add(itemizedOverlay2);
                    }

                    if (savedInstanceState2 == null) {

                        final MapController mc = mapView.getController();
                        if (!DataUtils.showPoint)
                            mc.animateTo(apr);
                        else
                        {
                            mc.animateTo(DataUtils.point);
                            DataUtils.point = null;
                            DataUtils.showPoint = false;
                        }
                        mc.setZoom(16);

                    } else {

                        // example restoring focused state of overlays
                        int focused;
                        focused = savedInstanceState2.getInt("focused_1", -1);
                        if (focused >= 0) {
                            itemizedOverlay.setFocus(itemizedOverlay.getItem(focused));
                        }
                    }

                    ActivityUtils.hideProgressDialog(Harta.this);
                }
            };
        registerReceiver(receivedAllStationsSuccessReceiver,receivedAllStationsSuccessIntentFilter);
        DBManager.getInstance().getAllLinesForAllStations();
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    @Override
	protected void onSaveInstanceState(Bundle outState) {

		// example saving focused state of overlays
		if (itemizedOverlay.getFocus() != null) outState.putInt("focused_1", itemizedOverlay.getLastFocusedIndex());
		super.onSaveInstanceState(outState);

	}
}
